#include "RunningSoldierPhysics.h"




void RunningSoldierPhysics::_initDirection()
{
	double x = _armyData->rect->X;
	double targetx = _armyData->targetRect->X;
	double direction;

	if (x > targetx) {
		direction = -2.5;
	} else {
		direction = 2.5;
	}
	_armyData->rect->Vy = -6;
	_armyData->rect->Vx = direction;
}

void RunningSoldierPhysics::_setToDead(int actiondata)
{
	_armyData->getRect()->Vx = _armyData->getRect()->Vy = 0;
	_armyData->canInteract = false;
	_armyData->delayForDead = _DATA_DELAY_FOR_DEAD;
}

void RunningSoldierPhysics::updateBeforeDead()
{
	_armyData->getRect()->X += 1;
	_armyData->getRect()->Y += 3;
	_armyData->delayForDead--;
	if (_armyData->delayForDead <= 0) {
		if (_armyData->rect->X - _armyData->targetRect->X > 500) {
			_armyData->canInteract = true;
			_armyData->rect->X = startx;
			_armyData->rect->Y = starty;
			_armyData->isDead = false;
			_initDirection();
		} else {
			_armyData->isDead = true;
		}
	}
}

RunningSoldierPhysics::RunningSoldierPhysics(ArmyData* armyData)
{
	_armyData = armyData;
	_armyData->getRect()->Height -= 20;
	_armyData->getRect()->Width -= 30;
	startx = _armyData->rect->X;
	starty = _armyData->rect->Y;
	_onJump = true;

	_initDirection();
}

void RunningSoldierPhysics::setOnRun()
{
	_onJump = false;
	_armyData->_actions = ACTIONS_JUMPING;
}

void RunningSoldierPhysics::setOnJump()
{
	_onJump = true;
	_armyData->_actions = ACTIONS_NONE;
}

void RunningSoldierPhysics::update(long time)
{
	if (_armyData->delayForDead > 0) {
		updateBeforeDead();
	} else if (_armyData->isActived && !_armyData->isDead) {
		if (_armyData->_actions == ACTIONS_STOP_FLYING) {
			setOnRun();
		} else if (_armyData->_actions == ACTIONS_JUMPING) {
			setOnJump();
		}
		// check if entity not on land, jump one time
		if (_onJump && _armyData->rect->Vy < 0) {
			_armyData->rect->Vy = 6;
		}
		if (_armyData->rect->Vy > 0) {
			_armyData->rect->Vy -= 0.2;
		} else {
			_armyData->rect->Vy = -6;
		}
		// update entity location
		_onJump = false;
		_armyData->rect->X += _armyData->rect->Vx;
		_armyData->rect->Y += _armyData->rect->Vy;		
	}
}

void RunningSoldierPhysics::setIsHurt(ACTIONS action, int action_data)
{
	if (action == ACTIONS_IS_HURT) {
		_setToDead(action_data);
	}
}
